<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class History extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		if(!$this->session->userdata('id')) return false;
		$this->db->where('user_id',$this->session->userdata('id'));
		$data['history'] = $this->db->get('history')->result();
		$this->load->view('layout_header');
		$this->load->view('history',$data);
		$this->load->view('layout_footer');
	}
	
	
	public function delete()
	{
		if(!$this->session->userdata('id')) return false;
		if($this->input->post())
		{
			foreach ($this->input->post('check') as $check) {
				$this->db->where('user_id', $this->session->userdata('id'));
				$this->db->where('id', $check);
				$this->db->delete('history'); 
			}
			redirect('history');
		}
		else
		{
			echo "error";
		}
	}
	public function delete_all()
	{
		if(!$this->session->userdata('id')) return false;
		if($this->input->post('okay')=='okay')
		{
			$this->db->where('user_id', $this->session->userdata('id'));
			$this->db->delete('history'); 
			redirect('history');
		}
		else
		{
			echo "error";
		}
	}
	
	
	
	
	public function log()
	{
		if(!$this->session->userdata('id')) return false;
		if($this->input->post())
		{
			$data = $this->input->post('data',TRUE);
			if($data)
			{
				$this->db->insert('history',array('user_id'=>$this->session->userdata('id'), 'data'=> $data));
			}
			else
			{
				echo "error";
			}
		}
		else
		{
			echo "error";
		}
	}
	public function autocomplete()
	{
		if(!$this->session->userdata('id')) return false;
		if($term = $this->input->get('term',TRUE))
		{
			$this->db->where('user_id', $this->session->userdata('id'));
			$this->db->like('data',$term);
			$data = $this->db->get('history')->result();
			if($data)
			{
				foreach ($data as $row) {
					$json[] = $row->data;
				}
				
			}
			echo json_encode($json);
		}
		else
		{
			echo "error";
		}
	}
	
	
}

/* End of file history.php */
/* Location: ./application/controllers/history.php */